class Bill < ActiveRecord::Base
  acts_as_paranoid

  validates_presence_of :name, :amount, :due, :user, :account
  validates_numericality_of :amount
  
  has_many   :payable_bills
  belongs_to :user
  belongs_to :account
  
  after_create :create_payable_bills
  
  def details
    "$#{amount} due on #{due.to_s(:short)}"
  end

protected
  def create_payable_bills
    amount = ((self.amount.to_f / User.find(:all).size) * 100).ceil.to_f / 100
    
    User.find(:all).each do |user|
      payable_bill = PayableBill.new(:bill => self, :user => user, :amount => amount)
      payable_bill.save
    end
  end
end
